package cn.monkey.gateway.stream.blacklist.async.redisson;

import cn.monkey.gateway.stream.blacklist.BlackEntity;
import cn.monkey.gateway.stream.blacklist.async.BlackEntityRepository;
import org.redisson.Redisson;
import org.redisson.api.RBloomFilter;
import reactor.core.publisher.Mono;

public class RedissonBloomFilterBlackEntityRepository implements BlackEntityRepository {

    private final RBloomFilter<String> bloomFilter;

    public RedissonBloomFilterBlackEntityRepository(Redisson redissonClient) {
        RBloomFilter<String> filter = redissonClient.getBloomFilter("blackList");
        filter.tryInit(1000_000, 0.03);
        this.bloomFilter = filter;
    }

    @Override
    public Mono<Boolean> containsKey(String key) {
        return Mono.just(this.bloomFilter.contains(key));
    }

    @Override
    public Mono<Void> add(BlackEntity blackListEntity) {
        return Mono.just(this.bloomFilter.add(blackListEntity.key())).then();
    }
}
